﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;

//自定义命名空间
using DreamCube.Foundation.Basic.Utility;
using DreamCube.Apps.Common.WinForm.Controls;

namespace AutoCompile.UI
{
    public partial class AddProject : Form
    {
        #region "属性"

        public List<Object[]> SelectProjects
        {
            get
            {
                List<Object[]> values = new List<Object[]>();
                if (dataGridView1.Rows != null && dataGridView1.Rows.Count > 0)
                {
                    for (Int32 i = 0; i < dataGridView1.Rows.Count; i++)
                        values.Add(new Object[] { dataGridView1.Rows[i].Cells[0].Value, dataGridView1.Rows[i].Cells[1].Value, dataGridView1.Rows[i].Cells[2].Value, dataGridView1.Rows[i].Cells[3].Value });
                }
                return values;
            }
        }

        public String OutputPath
        {
            get { return this.textBox1.Text;  }
        }

        public String FrameworkVersion
        {
            get { return this.comboBox1.Text; }
        }

        #endregion

        public AddProject()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 添加文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //openFileDialog1.Filter = "项目文件(*.csproj)|*.csproj|解决方案文件(*.sln)|*.sln";
            openFileDialog1.Filter = "项目文件(*.csproj)|*.csproj";
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.InitialDirectory = @"";
            openFileDialog1.Multiselect = false;
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                String item = openFileDialog1.FileName;
                if (item.RightOfLast(".").ToLower() == "sln") //选中的是解决方案文件
                {
 
                }
                else
                {
                    List<String> condictions = ProjectHelper.GetCondition(item);
                    List<String> configs = new List<String>();
                    List<String> platforms = new List<String>();
                    for (Int32 i = 0; i < condictions.Count; i++)
                    {
                        //'Release|AnyCPU'
                        String tempValue = condictions[i].Right("==").Trim();
                        String[] valuePair = tempValue.Substring(1, tempValue.Length - 2).Split('|');
                        configs.Add(valuePair[0]);
                        platforms.Add(valuePair[1]);
                    }
                    
                    //先判断是否已经添加了选中的项目
                    if (MyDataGridView.Contains(dataGridView1, new Int32[] { 1 }, item, StringComparison.CurrentCulture))
                    {
                        return;
                    }

                    Object[] val = new Object[] { item.RightOfLast("\\"), item, null, null };
                    Int32 index = dataGridView1.Rows.Add(val);
                    DataGridViewComboBoxCell cell1 = dataGridView1.Rows[index].Cells["ProjectConfig"] as DataGridViewComboBoxCell;
                    IList<String> sigleConfigs = new List<String>();
                    configs.RemoveRepeatItems<String>(ref sigleConfigs);
                    if (cell1 != null)
                    {
                        cell1.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
                        cell1.DisplayMember = "Name";
                        cell1.DataSource = sigleConfigs;
                    }

                    DataGridViewComboBoxCell cell2 = dataGridView1.Rows[index].Cells["Platform"] as DataGridViewComboBoxCell;
                    IList<String> siglePlatforms = new List<String>();
                    platforms.RemoveRepeatItems<String>(ref siglePlatforms);

                    if (cell2 != null)
                    {
                        cell2.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
                        cell2.DataSource = siglePlatforms;
                    }
                }
            }
        }

        /// <summary>
        /// 选择输出目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        /// <summary>
        /// 验证数据
        /// </summary>
        private Boolean ValidateData()
        {
            if (String.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("输出路径不能为空","提醒");
                return false;
            }

            if (comboBox1.SelectedIndex < 0)
            {
                MessageBox.Show("请选择要编译的版本", "提醒");
                comboBox1.Focus();
                return false;
            }
            for (Int32 i = 0; i < dataGridView1.Rows.Count; i++)
            {
                String pjConfig = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value);
                if (String.IsNullOrEmpty(pjConfig))
                {
                    MessageBox.Show("每一个选中的项目都必须配置好对应的选用的项目配置和编译的平台！", "提醒");
                    return false;
                }
                String plateformConfig = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value);
                if (String.IsNullOrEmpty(pjConfig))
                {
                    MessageBox.Show("每一个选中的项目都必须配置好对应的选用的项目配置和编译的平台！", "提醒");
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 确定按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddProject_Load(object sender, EventArgs e)
        {
            textBox1.Text = ConfigsHelper.GetOutputPath();
        }
    }
}
